// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-8-13

#include "stdafx.h"
#include ".\settingwnd.h"
#include <common/ui/brushwnd.h>
#include "settingpagectrl.h"
#include "gamesetting.h"

IMPLEMENT_DYNAMIC(CJoySettingWnd, CJoyWnd)

//##ModelId=44C4B5DC032C
CJoySettingWnd::CJoySettingWnd(void)
{
    m_name = "setting_window";
    m_skinArea  = true;

    m_btnClose      = NULL;
    m_settingCtrl   = NULL;
    m_gameSetting   = NULL;
}

//##ModelId=44C4B5DC032E
CJoySettingWnd::~CJoySettingWnd(void)
{
}

BEGIN_MESSAGE_MAP(CJoySettingWnd, CJoyWnd)
    ON_WM_CREATE()
    ON_WM_NCHITTEST()
    ON_WM_DESTROY()
END_MESSAGE_MAP()

//##ModelId=44C4B5DC0331
void CJoySettingWnd::OnOK()
{
    if(m_gameSetting != NULL)
    {
        m_gameSetting->SaveSetting();
    }
}

//##ModelId=44C4B5DC0318
void CJoySettingWnd::CM_CloseClick(CJoyButton* )
{
    CDialog* pParent = (CDialog*)GetParent();
    if(pParent != NULL)
    {
        pParent->EndDialog(IDCANCEL);
    }
}

//##ModelId=44C4B5DC031E
void CJoySettingWnd::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
    __super::CM_LoadSkin(pSkinElem);

    CWnd* pParent = GetParent();
    if(pParent != NULL)
    {
        pParent->MoveWindow(m_area);
        pParent->CenterWindow();
    }
}

//##ModelId=44C4B5DC033C
int CJoySettingWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CJoyWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    CJoyBrushWnd* pTopBack = new CJoyBrushWnd();
    pTopBack->m_name    = "top_back";
    pTopBack->Create(this, CRect(0, 0, 10, 10));
    pTopBack->m_hitTest = HTTRANSPARENT;
    
    CJoyBrushWnd* pBottomBack = new CJoyBrushWnd();
    pBottomBack->m_name = "bottom_back";
    pBottomBack->Create(this, CRect(0, 0, 10, 10));

    CJoyBrushWnd* pLeftBack = new CJoyBrushWnd();
    pLeftBack->m_name   = "left_back";
    pLeftBack->Create(this, CRect(0, 0, 10, 10));

    CJoyBrushWnd* pRightBack = new CJoyBrushWnd();
    pRightBack->m_name  = "right_back";
    pRightBack->Create(this, CRect(0, 0, 10, 10));

    m_btnClose = new CJoyButton();
    m_btnClose->m_name  = "close";
    m_btnClose->Create(pTopBack, CRect(0, 0, 10, 10));
	m_btnClose->SetClickEvent(this, (CJoyButton::PClickEvent)&CJoySettingWnd::CM_CloseClick);

    CJoyWndContainer* pSettingPane = new CJoyWndContainer();
    pSettingPane->m_name = "setting_pane";
    pSettingPane->SetAlign(AT_Client);
    pSettingPane->Create(this, CRect(0, 0, 10, 10));

    m_settingCtrl = new CJoySettingPageCtrl();
    m_settingCtrl->m_name = "setting_page_control";
    m_settingCtrl->SetAlign(AT_Client);
    m_settingCtrl->Create(pSettingPane, CRect(0, 0, 10, 10));

    m_gameSetting = new CJoyGameSetting();
    m_gameSetting->m_topDlg = (CJoySettingDlg*)GetParent();
    m_settingCtrl->AddPage("game", m_gameSetting);

    JoyGetManager()->RegWnd(this);
    return 0;
}

//##ModelId=44C4B5DC033F
LRESULT CJoySettingWnd::OnNcHitTest(CPoint point)
{
    CJoyWnd::OnNcHitTest(point);
    return (UINT)HTTRANSPARENT;
}

//##ModelId=44C4B5DC034D
void CJoySettingWnd::OnDestroy()
{
    JoyGetManager()->UnregWnd(this);

    CJoyWnd::OnDestroy();
}
